logo

The best it Trainig Institute In Gurgaon

Locators in Selenium

class-path

Write the TestClass in Sellenium

    Creating the Test Class:
  • Create a new Java class in "src/test/java" under an appropriate package.

Write the Test Script in Sellenium

class-space
    package asc;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.edge.EdgeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.safari.SafariDriver;
    import io.github.bonigarcia.wdm.WebDriverManag
    public class NT {
        
        public static String browser = "Chrome";
        public static WebDriver driv
        public static void main(String[] args) {
            // Setup WebDriver based on the selected browser
            if (browser.equals("Firefox")) {
                WebDriverManager.firefoxdriver().setup();
                driver = new FirefoxDriver(); 
            } else if (browser.equals("Chrome")) {
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver(); 
            } else if (browser.equals("Safari")) {
                WebDriverManager.safaridriver().setup();
                driver = new SafariDriver();
            } else if (browser.equals("Edge")) {
                WebDriverManager.edgedriver().setup();
                driver = new EdgeDriver();
        
            // Navigate to the Selenium documentation page
            driver.get("https://www.selenium.dev/documentation/webdriver/elements/locators/
            // Interact with the elements on the page
            driver.findElement(By.linkText("Getting Started")).click();
            driver.findElement(By.partialLinkText("Getting")).click
            // Close the browser
            driver.quit();
        }
    }
                    
    Output output

Locators in Selenium:

  • Locators are used in Selenium to find and interact with web elements on a web page. The following are common types of locators:
    ID
  • By.id("element_id")
  • Example (don't give space in the code): driver .findElement (By.id ("user-name")) .sendKeys ("standard_user");
    Name:
  • By.name ("element_name")
  • Example (don't give space in the code): driver .findElement (By.name ("username")) .sendKeys ("standard_user");
    Class Name:
  • By.className ("element_class")
  • Example (don't give space in the code): driver .findElement (By.className ("form_input")) .sendKeys ("standard_user");
    Tag Name:
  • By.tagName ("tag_name")
  • Example (don't give space in the code): driver .findElement (By.tagName ("input")) .sendKeys ("standard_user");
    Link Text:
  • By.linkText ("exact_link_text")
  • Example (don't give space in the code): driver .findElement (By .linkText ("Login")) .click();
    Partial Link Text:
  • By .partialLinkText ("partial_link_text")
  • Example (don't give space in the code): driver .findElement (By .partialLinkText ("Log")) .click();
    CSS Selector:
  • By .cssSelector ("element _css _selector")
  • Example (don't give space in the code): driver .findElement (By.cssSelector ("#user-name")) .sendKeys ("standard_user");
    XPath:
  • By .xpath ("element_xpath")
  • Example (don't give space in the code): driver. findElement (By.xpath ("//input[@id= 'user-name ']")) .sendKeys ("standard_user");